package com.github.ylt.utils;


import java.net.*;
import java.util.Enumeration;

/**
 * @author wutong
 */
public class IpUtils {

    private IpUtils(){}
    private static class SingletonHolder{
        private static final IpUtils INSTANCE = new IpUtils();
    }
    public static IpUtils getInstance(){
        return SingletonHolder.INSTANCE;
    }

    private String localhostIpv4(boolean v4){
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if(v4){
                        if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address && !networkInterface.getName().contains("docker") && !networkInterface.getName().contains("br-")) {
                            return inetAddress.getHostAddress();
                        }
                    }else{
                        if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address && !networkInterface.getName().contains("docker") && !networkInterface.getName().contains("br-")) {
                            return inetAddress.getHostAddress();
                        }

                    }
                }
            }
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    public Inet4Address getIpv4(boolean v4){
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
                while (inetAddresses.hasMoreElements()) {
                    InetAddress inetAddress = inetAddresses.nextElement();
                    if(v4){
                        if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address && !networkInterface.getName().contains("docker") && !networkInterface.getName().contains("br-")) {
                            return (Inet4Address) inetAddress;
                        }
                    }else{
                        if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address && !networkInterface.getName().contains("docker") && !networkInterface.getName().contains("br-")) {
                            return (Inet4Address) inetAddress;
                        }

                    }
                }
            }
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    public String localhostIpv6(){
        return localhostIpv4(false);
    }
    public String localhostIpv4(){
        return localhostIpv4(true);
    }

    public NetworkInterface getNetworkInterface(){
        try {
            Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
            while (networkInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = networkInterfaces.nextElement();
                if (networkInterface.isUp()) {
                    if (!networkInterface.isLoopback() && !networkInterface.isVirtual() && !networkInterface.getName().contains("docker") && !networkInterface.getName().contains("br-")) {
                        return networkInterface;
                    }
                }
            }
        } catch (SocketException e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(IpUtils.getInstance().localhostIpv6());
    }
}
